<?php
App::uses('AppController', 'Controller');
/**
 * News Controller
 */
class NewsController extends AppController {

    public $uses = array('Article');

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator');
    public $paginate = array(
		'conditions' => array('category_id' => 1),
        'limit' => 12,
        'order' => array(
            'Article.created_date' => 'DESC'
        )
    );
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Article->recursive = -1;
        $this->Paginator->settings = $this->paginate;
		$this->set('articles', $this->Paginator->paginate('Article'));
	}
    
/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function detail($id = null) {
		if (!$this->Article->exists($id)) {
			throw new NotFoundException(__('Invalid article'));
		}
		$options = array('conditions' => array('Article.' . $this->Article->primaryKey => $id));
		$this->set('article', $this->Article->find('first', $options));
        
        $this->Article->recursive = -1;
        $cond = array('conditions' => array('Article.' . $this->Article->primaryKey . ' <>' => $id, 'category_id' => 1), 'order' => 'created_date DESC', 'limit' => 5);
        $this->set('articleLastest', $this->Article->find('all', $cond));
	}
	
	public function search() {
		$keyword = $this->request->query('keyword');
		$this->Article->recursive = -1;
		$this->paginate = array(
			'conditions' => array('title LIKE' => '%' . $keyword . '%'),
			'limit' => 1,
			'order' => array(
				'Article.created_date' => 'DESC'
			)
		);
        $this->Paginator->settings = $this->paginate;
		
		$this->set('keyword', $keyword);
		$this->set('articles', $this->Paginator->paginate('Article'));
	}
}
